package com.yanqu.road.server.cmd.hunt;

import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.hunt.HuntProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.union.UnionModule;
import com.yanqu.road.server.gameplayer.module.union.cross.CrossUnionModule;
import com.yanqu.road.server.manger.config.HuntConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.hunt.HuntMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = Protocol.S_HUNT_SCENE_CHANGE, desc = "狩猎场景切换")
public class HuntSceneChangeCmd extends AbstractCommand {

	private int type;

	private List<HuntProto.HuntMessageTempMsg.Builder> msgList;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		HuntProto.HuntSceneChangeReqMsg reqMsg = HuntProto.HuntSceneChangeReqMsg.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(!SystemOpenMgr.systemOpen(player, eSystemId.Hunt.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if(!HuntConfigMgr.isHuntSystemOpenTime()){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if(1 == type){
			HuntMgr.removeUserInHuntScene(player.getUserId());
		}else {
			HuntMgr.addUserInHuntScene(player.getUserId());
			msgList = HuntMgr.getHuntMessageMsgList();
			String unionUid = player.getUserInfo().getUnionUid();
			if(!StringUtils.isNullOrEmpty(unionUid)){
				player.getModule(CrossUnionModule.class).syncChamberPatronsList();
			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		HuntProto.HuntSceneChangeRespMsg.Builder respMsg = HuntProto.HuntSceneChangeRespMsg.newBuilder();
		respMsg.setRet(ret);
		if (0 == ret) {
			if(null != msgList) {
				for (HuntProto.HuntMessageTempMsg.Builder message : msgList) {
					respMsg.addDataList(message);
				}
			}
		}
		player.sendPacket(Protocol.U_HUNT_SCENE_CHANGE, respMsg);
	}

}
